package com.pzb.wechat.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.pzb.common.core.domain.entity.SysDept;
import com.pzb.common.core.redis.RedisCache;
import com.pzb.common.utils.DateUtils;
import com.pzb.common.utils.SecurityUtils;
import com.pzb.common.utils.StringUtils;
import com.pzb.market.domain.MkIntegralSetting;
import com.pzb.market.service.IMkIntegralSettingService;
import com.pzb.order.domain.OdOrderSetting;
import com.pzb.order.service.IOdOrderSettingService;
import com.pzb.system.service.ISysDeptService;
import com.pzb.wechat.domain.WechatAuthorization;
import com.pzb.wechat.domain.WechatPickupSetting;
import com.pzb.wechat.mapper.WechatComponentMapper;
import com.pzb.wechat.service.IWechatAuthorizationService;
import com.pzb.wechat.service.IWechatComponentService;
import com.pzb.wechat.service.IWechatPickupSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.pzb.wechat.mapper.WechatAppletMapper;
import com.pzb.wechat.domain.WechatApplet;
import com.pzb.wechat.service.IWechatAppletService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 小程序商户Service业务层处理
 * 
 * @author Mr_Wu
 * @date 2022-01-07
 */
@Service
public class WechatAppletServiceImpl implements IWechatAppletService 
{
    @Autowired
    private WechatAppletMapper wechatAppletMapper;
    @Autowired
    private ISysDeptService deptService;

    @Autowired
    private RedisCache redisCache;
    @Autowired
    private IOdOrderSettingService orderSettingService;
    @Autowired
    private IMkIntegralSettingService integralSettingService;
    @Autowired
    private IWechatPickupSettingService pickupSettingService;
    @Autowired
    private IWechatComponentService wechatComponentService;
    @Autowired
    private IWechatAuthorizationService authorizationService;


    /**
     * 查询小程序商户
     * 
     * @param id 小程序商户主键
     * @return 小程序商户
     */
    @Override
    public WechatApplet selectWechatAppletById(Long id)
    {
        return wechatAppletMapper.selectWechatAppletById(id);
    }

    /**
     * 查询小程序商户
     *
     * @param appid 小程序商户appid
     * @return 小程序商户
     */
    public WechatApplet selectWechatAppletByAppid(String appid){
        return wechatAppletMapper.selectWechatAppletByAppid(appid);
    };

    /**
     * 查询小程序商户列表
     * 
     * @param wechatApplet 小程序商户
     * @return 小程序商户
     */
    @Override
    public List<WechatApplet> selectWechatAppletList(WechatApplet wechatApplet)
    {
        return wechatAppletMapper.selectWechatAppletList(wechatApplet);
    }

    /**
     * 新增小程序商户
     * 
     * @param wechatApplet 小程序商户
     * @return 结果
     */
    @Override
    @Transactional
    public int insertWechatApplet(WechatApplet wechatApplet)
    {
        wechatApplet.setCreateTime(DateUtils.getNowDate());
        //新增订单设置
        OdOrderSetting setting = new OdOrderSetting();
        setting.setExtAppid(wechatApplet.getAppid());
        orderSettingService.insertOdOrderSetting(setting);
        MkIntegralSetting integralSetting = new MkIntegralSetting();
        integralSetting.setExtAppid(wechatApplet.getAppid());
        integralSettingService.insertMkIntegralSetting(integralSetting);
        WechatPickupSetting pickupSetting = new WechatPickupSetting();
        pickupSetting.setExtAppid(wechatApplet.getAppid());
        pickupSettingService.insertWechatPickupSetting(pickupSetting);

        return wechatAppletMapper.insertWechatApplet(wechatApplet);
    }

    /**
     * 修改小程序商户
     * 
     * @param wechatApplet 小程序商户
     * @return 结果
     */
    @Override
    @Transactional
    public int updateWechatApplet(WechatApplet wechatApplet)
    {
        WechatApplet old = selectWechatAppletById(wechatApplet.getId());
        //如果更改extAppid
        if(!old.getAppid().equals(wechatApplet.getAppid())){
            //订单设置修改
            OdOrderSetting setting = orderSettingService.selectOdOrderSetting(old.getAppid());
            setting.setExtAppid(wechatApplet.getAppid());
            orderSettingService.updateOdOrderSetting(setting);
            //积分设置修改
            MkIntegralSetting integralSetting = integralSettingService.selectMkIntegralSetting(old.getAppid());
            integralSetting.setExtAppid(wechatApplet.getAppid());
            integralSettingService.updateMkIntegralSetting(integralSetting);
            //自提设置修改
            WechatPickupSetting pickupSetting = pickupSettingService.selectWechatPickupSetting(old.getAppid());
            pickupSetting.setExtAppid(wechatApplet.getAppid());
            pickupSettingService.updateWechatPickupSetting(pickupSetting);
        }
        return wechatAppletMapper.updateWechatApplet(wechatApplet);
    }

    /**
     * 删除小程序商户信息
     * 
     * @param id 小程序商户主键
     * @return 结果
     */
    @Override
    public int deleteWechatAppletById(Long id)
    {
        WechatApplet applet = selectWechatAppletById(id);
        //删除订单设置
        orderSettingService.deleteOrderSetting(applet.getAppid());
        //删除积分设置
        integralSettingService.deleteIntegralSetting(applet.getAppid());
        pickupSettingService.deleteWechatPickupSetting(applet.getAppid());
        return wechatAppletMapper.deleteWechatAppletById(id);
    }

    @Override
    public boolean checkExist(String appid,Long id) {
        int count = wechatAppletMapper.checkExist(appid,id);
        return count > 0 ? true : false;
    }

    @Override
    public List<WechatApplet> selectWechatAppletListByDeptIds(List<Long> deptIds) {
        return wechatAppletMapper.selectWechatAppletListByDeptIds(deptIds);
    }

    @Override
    public List<WechatApplet> getAuthorityAppList() {
        //查询所辖店铺
        //查询有权限的部门
        List<SysDept> deptList = deptService.selectDeptList(new SysDept());
        List<Long> deptIds= new ArrayList<>();
        for (SysDept sysDept : deptList) {
            deptIds.add(sysDept.getDeptId());
        }
        //查询所辖店铺
        List<WechatApplet> appletList = selectWechatAppletListByDeptIds(deptIds);
        String extAppid = redisCache.getCacheObject(SecurityUtils.getUserId().toString());
        if(appletList.size()>0){
            if(StringUtils.isNull(extAppid) || "1".equals(extAppid)){
                redisCache.setCacheObject(SecurityUtils.getUserId().toString(),appletList.get(0).getAppid());
            }
        }else {
            redisCache.setCacheObject(SecurityUtils.getUserId().toString(),"1");
        }
        return appletList;
    }

    @Override
    public String getExtAppid() {
        String extAppid = redisCache.getCacheObject(SecurityUtils.getUserId().toString());
        return extAppid == null ? "1" : extAppid;
    }

    @Override
    public void setExtAppid(String extAppid) {
        redisCache.setCacheObject(SecurityUtils.getUserId().toString(),extAppid);
    }

    @Override
    public void refreshApplet(String extAppid) {
        //获取授权方账号信息
        JSONObject auth= wechatComponentService.getAuthorizerInfo(extAppid);
        JSONObject js = auth.getJSONObject("authorizer_info");
        WechatApplet applet = selectWechatAppletByAppid(extAppid);
        if(applet!=null){
            applet.setNickName(js.getString("nick_name"));
            applet.setPrincipalName(js.getString("principal_name"));
            applet.setSignature(js.getString("signature"));
            JSONObject business_info = js.getJSONObject("business_info");
            applet.setOpenPay(business_info.getString("open_pay"));
            updateWechatApplet(applet);
        }
    }
}
